package primetest.impl.controler;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import primetest.dto.BedDto;
import primetest.dto.TermDto;
import primetest.dto.WardDto;
import primetest.faces.BedListModel;
import primetest.impl.manager.SessionManager;
import primetest.impl.service.BedService;
import primetest.impl.service.WardService;

@Named("bedListControler")
@SessionScoped
public class BedListControler implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -2341239310873546662L;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Inject
	private SessionManager sessionManager;

	private BedListModel bedList = null;
	private List<BedDto> baseBedList;
	private BedDto selectedBed = new BedDto();
	private BedDto backBed = new BedDto();
	private BedDto newBed = new BedDto();
	private int first;
	private boolean editMode = false;
	private boolean addMode = false;

	private List<String> statusList;

	private List<BedDto> filteredTerms;

	public boolean getEditMode() {
		return editMode;
	}

	public boolean getAddMode() {
		return addMode;
	}

	@Inject
	private BedService bedService;

	@Inject
	private WardService wardService;

	public List<BedDto> getBaseBedList() {
		if (baseBedList == null) {
			try {
				baseBedList = bedService.getBedList();
				Collections.sort(baseBedList, new Comparator<BedDto>() {
					@Override
					public int compare(BedDto a, BedDto b) {
						return a.getName().compareToIgnoreCase(b.getName());
					}
				});

				bedList = new BedListModel(baseBedList);
				wardService.getWardList();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		return baseBedList;
	}

	public BedListModel getBedList() {
		if (baseBedList == null) {
			try {
				baseBedList = bedService.getBedList();
				Collections.sort(baseBedList, new Comparator<BedDto>() {
					@Override
					public int compare(BedDto a, BedDto b) {
						return a.getName().compareToIgnoreCase(b.getName());
					}
				});
				bedList = new BedListModel(baseBedList);
				wardService.getWardList();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		return baseBedList == null ? new BedListModel(
				new ArrayList<BedDto>()) : bedList;
	}

	public void deleteBed() throws IOException {
		bedService.deleteBed(selectedBed.getKey());
		wardService.deleteBed(selectedBed);
	}

	public void reload() {
		baseBedList = null;
	}

	public BedDto getSelectedBed() {
		return selectedBed;
	}

	public void setSelectedBed(BedDto selectedBed) {
		this.selectedBed = selectedBed;
	}

	public int getFirst() {
		return first;
	}

	public void setFirst(int first) {
		this.first = first;
	}

	public BedDto getNewBed() {
		return newBed;
	}

	public void setNewBed(BedDto newBed) {
		this.newBed = newBed;
	}

	public void startEditBed() {
		editMode = true;
		backBed.setAll(selectedBed);
	}

	public void saveEditBed() {
		try {
			if (selectedBed.isValid()) {
				bedService.updateBed(selectedBed);
				editMode = false;
			} else {
				sessionManager.addGlobalMessageFatal("Floors invalid", null);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void cancelEditBed() {
		editMode = false;
		selectedBed.setAll(backBed);
	}

	public void startAddBed() {
		addMode = true;
		newBed = new BedDto();
	}

	public void addNewBed() throws IOException {
		if (newBed.isValid()) {
			WardDto wardDto = wardService.findWard(newBed.getWardKey());
			bedService.addBed(newBed);
			//baseBedList.add(newBed);
			addMode = false;
		} else {
			sessionManager.addGlobalMessageFatal("Floors invalid", null);
		}

	}

	public void cancelAddBed() {
		addMode = false;
		newBed = new BedDto();
	}

	public List<BedDto> getFilteredTerms() {
		return filteredTerms;
	}

	public void setFilteredTerms(List<BedDto> filteredTerms) {
		this.filteredTerms = filteredTerms;
	}

	public List<String> getStatusList() {
		if (statusList == null) {
			statusList = new ArrayList<String>();
			statusList.add("Free");
			statusList.add("Reserved");
			statusList.add("Occupied");
			statusList.add("Cleaning");
		}
		return statusList;
	}

	public void setStatusList(List<String> statusList) {
		this.statusList = statusList;
	}
}
